package com.shop.shard.controller.rest.user;

import com.shop.shard.common.*;
import com.shop.shard.controller.assembler.user.UserAddressAssembler;
import com.shop.shard.controller.command.user.UserAddressCreateCommand;
import com.shop.shard.controller.command.user.UserAddressUpdateCommand;
import com.shop.shard.entity.user.UserAddress;
import com.shop.shard.vo.user.UserAddressVO;
import com.shop.shard.service.user.UserAddressService;
import com.shop.shard.manager.user.UserAddressManager;
import com.shop.shard.mapper.user.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 代码生成器自动生成
 * @author:
 * @Createed Date: 2019-8-16 10:06:57
 * @ModificationHistory: Who  When  What
 * ---------     -------------   --------------------------------------
 **/
@RestController
@RequestMapping("api/v1/user/userAddress")
@Api(value = "userAddressService", description = "userAddress相关AIP")
public class UserAddressRest  {

    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private UserAddressManager userAddressManager;


    @PostMapping("add")
    @ApiOperation(value = "add UserAddress ", notes = "xxx")
    public InvokeResult add(@RequestBody @Valid UserAddressCreateCommand command) {
        UserAddress entity = UserAddressAssembler.toUserAddress(command);
        userAddressService.addEntity(entity);
        return InvokeResult.success();
    }

    /**
    * delect UserAddress by id
    * @return
    */
    @PostMapping("delete")
    @ApiOperation(value = "delect UserAddress by id", notes = "xxx")
    public InvokeResult delete(@RequestBody CommonDTO commonDTO){
        userAddressService.delete(commonDTO.getIds());
        return InvokeResult.success();
    }


    @PostMapping("update")
    @ApiOperation(value = "update UserAddress by id", notes = "xxx")
    public InvokeResult update(@RequestBody @Valid UserAddressUpdateCommand command) {
        UserAddress entity = UserAddressAssembler.toUserAddress(command);
        userAddressService.updateEntity(entity);
        return InvokeResult.success();
    }

    @GetMapping("detail")
    @ApiOperation(value = "get UserAddress detail by id", notes = "xxx")
    public InvokeResult detail(@RequestParam() Integer id) {
        UserAddressVO vo = userAddressManager.selectDetailVoById(id);
        return InvokeResult.success(vo);
    }

    @GetMapping("page")
    @ApiOperation(value = "get UserAddress page ", notes = "xxx")
    public IPage<UserAddressVO> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize){
        Page page = new Page(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>(4);
        IPage<UserAddressVO> iPage=  userAddressManager.getPage(page,map);
        return iPage;
    }
}
